#
# Makefile for the FREAX-kernel.
#
# Note! Dependencies are done automagically by 'make dep', which also
# removes any old dependencies. DON'T put your own dependencies here
# unless it's something special (ie not a .c file).
#


# $(MAKEFILE_LIST): 这是一个GNU make的自动变量，它包含了make所读取的所有makefile文件路径和名称列表。
# $(firstword $(MAKEFILE_LIST)): firstword函数返回列表中的第一个单词，这里用于获取首个被读取到的makefile文件名。通常情况下，如果我们在项目根目录下执行make命令，则此处获取到的就是根目录下的主Makefile文件。
# $(realpath $(firstword $(MAKEFILE_LIST))): realpath函数用于消除文件路径中可能存在的符号链接、相对路径等问题，并返回规范化后的绝对路径。这里将返回主Makefile文件在系统中真实的绝对路径。
# dirname $(realpath $(firstword $(MAKEFILE_LIST))): dirname是Unix shell命令，用于剥离非目录部分（比如说最后面那个文件名）,只留下目录部分。
# ROOT_DIR:= ...: :=这种方式赋值为直接扩展赋值（immediate expansion），即不考虑延迟求值，马上使用当前变量就行替换。会保证ROOT_DIR只被赋值一次，而且确保立即计算出正确结果。

ROOT_DIR:= $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

# 打印ROOT_DIR的值
$(info ROOT_DIR=$(ROOT_DIR))

include $(ROOT_DIR)/header.mk

LDFLAGS	+= -r

# NOTE!: do not try to add -On to CFLAGS with gcc4.x, which will optimize the
# memcpy function a lot to let the kernel not work, for fixing this problem,
# please refer to glibc and rewrite the memcpy in include/string.h, or just not
# use any -On options with gcc 4.x when compiling it. in fact, we only can not
# use -On with gcc4.x to compile fork.c, but after that you need to isolate the
# compling procedure of it, it's not good too. for playing with linux-0.11, we
# just have fun, have no much concerning about the performance.

#CFLAGS	+= -I../include
#CPP	+= -I../include

CFLAGS += $(shell find $(ROOT_DIR)/include -type d -exec echo -I{} \;)
CXXFLAGS += $(shell find $(ROOT_DIR)/include -type d -exec echo -I{} \;)


$(info CFLAGS=$(CFLAGS))
$(info CXXFLAGS=$(CXXFLAGS))


OBJDIR=./build
$(shell mkdir -p $(OBJDIR))

SRC = $(wildcard *.c *.s)
OBJS = $(patsubst %.s,$(OBJDIR)/%.o, $(patsubst %.c,$(OBJDIR)/%.o,$(SRC)))
$(info OBJS=$(OBJS))


$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<
#.c.o:
#	@$(CC) $(CFLAGS) -c -o $*.o $<

#$(OBJDIR)/%.s: %.c
#	$(CC) $(CFLAGS) -S -o $@ $<
#.c.s:
#	@$(CC) $(CFLAGS) -S -o $*.s $<

$(OBJDIR)/%.o: %.s
	$(AS) -o $@ $<
#.s.o:
#	@$(AS) -o $*.o $<

